#include"http.hpp"
#define WWWROOt "./wwwroot"

std::string Requststr(const HttpRequest &req)
{
    std::stringstream ss;
    ss<<req._method<<' '<<req._path<<' '<<req._version<<"\r\n";
    for(const auto& it:req._params)//参数
        ss<<it.first<<": "<<it.second<<"\r\n";
    for(const auto& it:req._headers)
        ss<<it.first<<": "<<it.second<<"\r\n";
    ss<<"\r\n";
    //DEBUG_LOG("请求正文:%s",req._body.c_str());
    ss<<req._body;
    return ss.str();
}

void Hello(const HttpRequest &req, HttpResponse &rsp)
{
    rsp.SetContent(Requststr(req),"text/plain");
}

void Login(const HttpRequest &req, HttpResponse &rsp)
{
    rsp.SetContent(Requststr(req),"text/plain");
}

void PutFile(const HttpRequest &req, HttpResponse &rsp)
{
    std::string path_name=WWWROOt+req._path;
    Util::WriteFile(path_name,req._body);
}

void DeleteFile(const HttpRequest &req, HttpResponse &rsp)
{
    rsp.SetContent(Requststr(req),"text/plain");
}
int main()
{
    HttpServer _server(8888);
    _server.SetThreadCount(2);
    _server.SetBaseDir(WWWROOt);//设置静态资源根目录
    _server.Get("/hello",Hello);
    _server.Post("/login",Login);
    _server.Put("/1234.txt",PutFile);
    _server.Delete("/1234.txt",DeleteFile);
    _server.Listen();
    return 0;
}